/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.display;

import plash.geom.Rectangle;
import plash.utils.ColorMapper;
import processing.core.PApplet;

/**
 *
 * @author User
 */
public class Stage extends PApplet
{
    protected int __backgroundColor = 0xFFFFFF;
    protected ColorMapper __color;
    protected Rectangle __size;
    protected DisplayObjectContainer __root;
    
    public Stage()
    {        
        this(new Rectangle(0,0,500,500), 0xFFFFFF);
    }
    
    public Stage(Rectangle stageSize, int backgroundColor)
    {        
        _initialize(stageSize, backgroundColor);
    }
    
    private void _initialize(Rectangle stageSize, int backgroundColor)
    {
        __color = new ColorMapper(backgroundColor);
        __size = stageSize;
        __backgroundColor = __color.getRGB();
        __root = new Sprite();
        __root.setIsRoot(true);
        __root.setName("_root");        
        __root.setPainter(this);
    }

    public DisplayObject addChild(DisplayObject child)
    {
        return __root.addChild(child);
    }

    public DisplayObject removeChild(DisplayObject child)
    {
        return __root.removeChild(child);
    }
    
    public DisplayObject getChildByName(String searchName)
    {
        return __root.getChildByName(searchName);
    }
    
    public DisplayObject getChildByIndex(int index) throws IndexOutOfBoundsException
    {
        return __root.getChildByIndex(index);
    }
    
    public boolean contains(DisplayObject toSearch)
    {
        return __root.contains(toSearch);
    }
    
    @Override
    public void setup()
    {
        size(__size.getWidth(), __size.getHeight());
        background(__color.getRed(), __color.getGreen(), __color.getBlue(), __color.getAlpha());
    }

    @Override
    public void draw()
    {
        if (__root == null)
        {
            return;
        }
        background(__color.getRed(), __color.getGreen(), __color.getBlue(), __color.getAlpha());
        __root.draw();
    }

    public int getBackgroundColor()
    {
        return __backgroundColor;
    }

    public Rectangle getStageSize()
    {
        return __size;
    }

    public DisplayObjectContainer getRoot()
    {
        return __root;
    }
}
